<script setup lang="ts">
import { onBeforeRouteLeave } from "vue-router";
import { RouterRecord } from "@/global/record/RouterRecord";
import { getAuthRoleApi, updateAuthRoleApi } from "@/api/system/user";

const route = useRoute();

const router = useRouter();

const { proxy } = getCurrentInstance();

// 加载
const loading = ref(false);

// 角色ID列表
const roleIds = ref([]);

// 角色列表
const roles = ref([]);

const form = ref({
  nickName: null,
  userName: null,
  userId: null
});

//总条数
const total = ref(0);

function getData() {
  const userId = route.params && route.params.userId;
  if (userId) {
    loading.value = true;
    getAuthRoleApi(userId).then(response => {
      form.value = response.user;
      roles.value = response.roles;
      nextTick(() => {
        roles.value.forEach((row, index) => {
          row.colNo = index + 1;
          if (row.flag) {
            proxy.$refs["roleRef"].toggleRowSelection(row);
          }
        });
      });
      loading.value = false;
    });
  }
}

/** 单击选中行数据 */
function clickRow(row) {
  proxy.$refs["roleRef"].toggleRowSelection(row);
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  roleIds.value = selection.map(item => item.roleId);
}


/** 提交按钮 */
function submitForm() {
  const authQuery: RoleAuthRecord = {
    userId: form.value.userId,
    roleIds: roleIds.value,
  }

  updateAuthRoleApi(authQuery).then(() => {
    proxy.$modal.msgSuccess("授权成功");
    close();
  })
}

/** 关闭按钮 */
function close() {
  router.back();
}

onBeforeRouteLeave(() => {
  useTagsViewStore().delView(<RouterRecord>route);
})

getData();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <h4 class="form-header h4">基本信息</h4>
      <el-form :model="form" label-width="80px">
        <el-row>
          <el-col :span="8" :offset="2">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="2">
            <el-form-item label="登录账号" prop="userName">
              <el-input v-model="form.userName" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <h4 class="form-header h4">角色信息</h4>
      <el-table v-loading="loading" row-key="roleId" @row-click="clickRow" ref="roleRef"
                @selection-change="handleSelectionChange"
                :data="roles">
        <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
        <el-table-column label="序号" align="center" width="55" prop="colNo" />
        <el-table-column label="角色编号" align="center" prop="roleId" />
        <el-table-column label="角色名称" align="center" prop="roleName" />
        <el-table-column label="权限字符" align="center" prop="roleKey" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="line-magin" />
      <div class="container-bottom">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </div>
      <div class="line-magin" />
    </div>
  </div>
</template>

<style scoped lang="scss">
.container-bottom {
  text-align: center;
}
</style>